package it.alertManager.services;

import it.alertManager.activity.AlertManagerMainActivity;
import it.alertManager.activity.R;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

public class AlertManagerService extends Service{

	NotificationManager mNM;
	AlarmManager alarmManager;
	
    @Override
    public void onCreate() {
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        // Display a notification about us starting.  We put an icon in the status bar.
        showNotification();
    }

    
    
    @Override
	public void onDestroy() {
        // Cancel the notification -- we use the same ID that we had used to start it
        mNM.cancel(R.layout.main);

        // Tell the user we stopped.
        Toast.makeText(this, "Sveglia disattivata", Toast.LENGTH_SHORT).show();
	}



	private void showNotification(){
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = "Il servizio di alerting è partito....";

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(android.R.drawable.ic_lock_idle_alarm, text,
                System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, AlertManagerMainActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, "Notifica presente",  text, contentIntent);

        // Send the notification.
        // We use a layout id because it is a unique number.  We use it later to cancel.
        mNM.notify(R.layout.main, notification);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, null);
    }

	@Override
	public IBinder onBind(Intent intent) {
        /*if (IAlertManagerService.class.getName().equals(intent.getAction())) {
        }*/
        return mBinder;
	}
	
    /**
     * The IRemoteInterface is defined through IDL
     */
    private final IAlertManagerService.Stub mBinder = new IAlertManagerService.Stub() {
	    public int wakeUpBoy() throws RemoteException {
	    	// TODO Auto-generated method stub
	    	return 0;
	    }
    };
}
